package com.bsoft.gol.editor.dao;

import com.bsoft.gol.editor.editor.base.EditorFormQr;
import com.bsoft.gol.editor.editor.qo.QueryEditorFormQrQo;
import com.bsoft.gol.editor.utils.CommonUtils;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import org.atag.dao.action.QoHibernateStatelessResultAction;
import org.atag.dao.model.PageInfo;

import java.util.List;

/**
 * @program: Myproject
 * @description: 表单二维码
 * @author: zangsf
 * @create: 2022-12-17 13:19
 **/
public abstract class EditorFormQrDAO extends ctd.persistence.support.hibernate.HibernateSupportDelegateDAO<EditorFormQr> {

    public EditorFormQrDAO() {
        this.setKeyField("editorQrId");
    }

    //这里查询个人三个维度的表单,1"自己管理机构下的,自己机构科室下的->这两个直接按照科室查询即可",自己创建的
    public PageInfo<EditorFormQr> queryEditorFormQr(final QueryEditorFormQrQo qo) {
        QoHibernateStatelessResultAction<EditorFormQr, QueryEditorFormQrQo> action = new QoHibernateStatelessResultAction(true, qo, EditorFormQr.class);
        action.setSql("SELECT  {{" +
                "  editor_qr_id editorQrId,  " +
                "  questionnaire_id questionnaireId,  " +
                "  form_type formType,  " +
                "  org_id orgId,  " +
                "  org_name orgName,  " +
                "  object_type objectType,  " +
                "  object_id objectId,  " +
                "  object_name objectName, " +
                "object_photo objectPhoto," +
                "object_level as objectLevel, " +
                "  qr_url qrUrl,  " +
                "  `status`,  " +
                "  create_user_id createUserId,  " +
                "  create_user_name createUserName,  " +
                "  create_time createTime   " +
                " }} FROM  " +
                "  editor_form_qr   " +
                "WHERE  " +
                "  1 = 1  ");
        if (CommonUtils.isNotEmpty(qo.getQuestionnaireId())){
            action.addCondition("and  questionnaire_id=:questionnaireId ");
        }
        if (CommonUtils.isNotEmpty(qo.getKeyword())){
            qo.setKeyword("%"+qo.getKeyword()+"%");
            qo.setKeyword1("%"+qo.getKeyword());
            action.addCondition("and  (object_id like:keyword or object_name like:keyword1 ) ");
        }
        action.addSql("  order by create_time desc");
        HibernateSessionTemplate.instance().execute(action);
        return action.getResultPage();
    }

    @DAOMethod(sql = "update EditorFormQr set status =:status where editorQrId =:editorQrId ")
    public abstract void updateStatus(@DAOParam("editorQrId") String  editorQrId,@DAOParam("status") byte  status );


    @DAOMethod(sql = "from EditorFormQr where objectType =:objectType and orgId =:orgId and  questionnaireId =:questionnaireId and  objectId =:objectId ")
    public abstract EditorFormQr getStatus(@DAOParam("objectType") byte  objectType,@DAOParam("orgId") String  orgId,@DAOParam("questionnaireId") String  questionnaireId,@DAOParam("objectId") String  objectId );


 @DAOMethod(sql = " select qrUrl from EditorFormQr where editorQrId in:editorQrIdList ")
    public abstract List<String> findQrUrl(@DAOParam("editorQrIdList") List<String>  editorQrIdList );

}
